﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.Linq;

namespace DAL
{
    public class DaCTPhanLoaiSP : dBConnect
    {
        #region "Get all infomation in table mp_CTPLSP............."
        public static List<CTPhanLoaiSPDto> GetAllCTPhanLoaiSP()
        {
            List<CTPhanLoaiSPDto> ctPLSanPham = new List<CTPhanLoaiSPDto>();
            var result = from k in db.mp_CTPLSPs
                         select new CTPhanLoaiSPDto(k.ID_PLSP, k.Ten_CTPLSP,new PLSanPhamDto(k.ID_PLSP,k.mp_PLSanPham.TenPLSP))
                         {
                             IDChiTietPLSP = k.ID_CTPLSP,
                             TenCTPLSP = k.Ten_CTPLSP,
                             PLSP = new PLSanPhamDto(k.ID_PLSP,k.mp_PLSanPham.TenPLSP)
                         };
            ctPLSanPham = result.ToList<CTPhanLoaiSPDto>();
            return ctPLSanPham;
        }
        #endregion

        #region "Get one CTPhanLoaiSP in table mp_CTPLSP............."
        public static List<CTPhanLoaiSPDto> GetOneCTPhanLoaiSanPhamByID(CTPhanLoaiSPDto ctPhanLoaiSP)
        {
            //List<NhanHieuDto> nhanHieu = new List<NhanHieuDto>();
            var result = from k in db.mp_CTPLSPs
                         where (k.ID_CTPLSP == ctPhanLoaiSP.IDChiTietPLSP)
                         select new CTPhanLoaiSPDto(k.ID_CTPLSP, k.Ten_CTPLSP,new PLSanPhamDto(k.ID_PLSP,k.mp_PLSanPham.TenPLSP))
                         {
                             IDChiTietPLSP = k.ID_CTPLSP,
                             TenCTPLSP = k.Ten_CTPLSP,
                             PLSP = new PLSanPhamDto(k.ID_PLSP,k.mp_PLSanPham.TenPLSP)
                         };
            return result.ToList<CTPhanLoaiSPDto>();
        }
        #endregion

        #region "Insert, Update for CTPhanLoaiSP.............."
        public static string mp_CTPhanLoaiSP_InsertUp(CTPhanLoaiSPDto ctPhanLoaiSP)
        {
            mp_CTPLSP o;
            string IdCTPLSP = ctPhanLoaiSP.IDChiTietPLSP.ToString();
            if (!string.IsNullOrEmpty(IdCTPLSP))
            {
                o = new mp_CTPLSP();
            }
            else
            {
                o = db.mp_CTPLSPs.Where(j => j.ID_CTPLSP == ctPhanLoaiSP.IDChiTietPLSP).SingleOrDefault();
            }
            if (!string.IsNullOrEmpty(ctPhanLoaiSP.IDChiTietPLSP.ToString()))
                o.ID_CTPLSP = ctPhanLoaiSP.IDChiTietPLSP;
            if (!string.IsNullOrEmpty(ctPhanLoaiSP.TenCTPLSP))
                o.Ten_CTPLSP = ctPhanLoaiSP.TenCTPLSP;
            if (!string.IsNullOrEmpty(ctPhanLoaiSP.PLSP.IDPLSanPham.ToString()))
                o.ID_PLSP = ctPhanLoaiSP.PLSP.IDPLSanPham;
            if (!string.IsNullOrEmpty(IdCTPLSP))
            {
                db.mp_CTPLSPs.InsertOnSubmit(o);
            }
            db.SubmitChanges();
            return o.ID_CTPLSP.ToString();
        }
        #endregion

        #region "Delete one CTPhanLoaiSanPham..............."
        public static void mp_CTPhanLoaiSP_Del(CTPhanLoaiSPDto ctPhanLoaiSP, bool isPermanent)
        {
            var o = db.mp_CTPLSPs.Where(j => j.ID_CTPLSP == ctPhanLoaiSP.IDChiTietPLSP).SingleOrDefault();
            if (o != null)
            {
                if (isPermanent)
                    db.mp_CTPLSPs.DeleteOnSubmit(o);
                db.SubmitChanges();
            }
        }
        #endregion
    }
}
